#include "device_file_cache.h"
#include "ui_device_file_cache.h"
#include <QtDebug>
#include <QSettings>
#include "../../public_opt_subs/typedef_mdle/check_blck/QTypedefCheckHex.h"
device_file_cache::device_file_cache(QWidget *parent) :
    device_mdle(parent),
    ui(new Ui::device_file_cache)
{
    ui->setupUi(this);
    this->hide_dev(true);
    // --------------------------------------------
    // 修改默认的界面信息
    table->setItem(1-1, 1-1, new QTableWidgetItem(QString("dev_name")));
    table->setItem(1-1, 2-1, new QTableWidgetItem(QString("device_file_cache")));
    table->setItem(2-1, 1-1, new QTableWidgetItem(QString("dev_code")));
    table->setItem(2-1, 2-1, new QTableWidgetItem(QString("00000002")));
    table->setItem(7-1, 1-1, new QTableWidgetItem(QString("dev_handle")));
    table->setItem(7-1, 2-1, new QTableWidgetItem(QString("device_file_cache1")));

    // --------------------------------------------
    // 重新编写IO
    table->setItem(1-1, 3-1, new QTableWidgetItem(QString("itf_in_num")));
    table->setItem(1-1, 4-1, new QTableWidgetItem(QString("1")));
    table->setItem(2-1, 3-1, new QTableWidgetItem(QString("itf_in1")));
    table->setItem(2-1, 4-1, new QTableWidgetItem(QString("s_fil")));

    table->setItem(1-1, 5-1, new QTableWidgetItem(QString("itf_out_num")));
    table->setItem(1-1, 6-1, new QTableWidgetItem(QString("0")));
    // --------------------------------------------
    // 增加逻辑
    s_interface_fil = new interface_fil_slaver;
    connect(s_interface_fil,&interface_mdle::itf_in_tx,
            this,&device_mdle::itf_in1_tx);
    connect(this,&device_mdle::itf_in1_rx,
            s_interface_fil,&interface_mdle::itf_in_rx);
    //内部信号监测
    connect(this,&device_mdle::itf_in1_tx,
            this,&device_mdle::signal_itf_monitor_slot);
    connect(this,&device_mdle::itf_in1_rx,
            this,&device_mdle::signal_itf_monitor_slot);

    //内部触发逻辑
    connect(s_interface_fil,&interface_fil_slaver::recv_fil_trig,
            this,&device_file_cache::wait_salver_recv_slot);
    // --------------------------------------------
    // 创建内部逻辑
    ui_dir = new QFrameDirsBrowser();
    ui->layout_page->addWidget(ui_dir); //显示到目录
    QSettings *settings;
    QString setting_ini = QApplication::applicationDirPath() + "/user_data/setting.ini";
    settings = new QSettings(setting_ini,QSettings::IniFormat);
    file_work_dir = settings->value("work_dir","").toString(); //获取当前目录
    if(file_work_dir != "")
    {
        file_work_dir = file_work_dir+"/dev_workspace/fil_itf/"+this->getHandle();
        QString work_path = file_work_dir;
        QDir dir;
        bool ok = dir.mkpath(work_path); //创建目录
        QDEBUG << work_path << ok;
        ui_dir->onPathChanged(work_path);
    }
}

device_file_cache::~device_file_cache()
{
    delete ui;
}

void device_file_cache::wait_salver_recv_slot(itf_fil itf)
{

    if(s_interface_fil->get_recv_fil_type() == "list")
    {

        QStringList listPath = ui_dir->getAllFilePath("bit", sizeof(itf.addr) / sizeof(quint32));
        QTypedefCheckHex chk;
        if(chk.is_hex(listPath)) //检查目录是否正确，防止异常加载，后续可以剔除对应的目录
        {
            QDEBUG << listPath;
        }
        s_interface_fil->slaver_send_list(listPath); //直接查询缓存器中上传过的文件
    }
    else if(s_interface_fil->get_recv_fil_type() == "save")
    {
        QString baseName = s_interface_fil->slaver_recv_save(file_work_dir);
        QDEBUG << file_work_dir;
        s_interface_fil->slaver_send_save(file_work_dir,baseName);
    }
    else if(s_interface_fil->get_recv_fil_type() == "load")
    {
        QString baseName = s_interface_fil->slaver_recv_load(file_work_dir);
        s_interface_fil->slaver_send_load(file_work_dir,baseName);
    }


}
